package com.handinfo.uimenu;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

public class UIPopmenu extends UIAbstractContext
{

	public static final int ALIGN_LEFT = 0;
	public static final int ALIGN_CENTRE = 1;
	public static final int ALIGN_RIGHT = 2;
	/**
	 * 内容层离rect层的宽度
	 */
	private final int IW = UITools.IW_RECT_CONTENT;

	private final int IH = 2;// 框与字之间的间隔

	private String[] m_menus;
	private String[][] m_strs;
	private int m_index_menu;// 选中索引
	private int m_anchor;
	/**
	 * 选择框的宽度
	 */
	private int m_w_rect;

	protected UIPopmenu(int id)
	{
		super(id);
	}

	public void setPopmenus(String[] menus)
	{
		setPopmenus(menus, ALIGN_LEFT);
	}

	/**
	 * 设置的menus只能是普通字符串数组而非超链接类型
	 * @param menus
	 * @param anchor
	 */
	public void setPopmenus(String[] menus, int anchor)
	{
		if (menus == null) {
//			throw new IllegalArgumentException("未传入参数");
			Tools.debugPrintln("未传入参数");
			return ;
		}
		m_menus = menus;
		m_index_menu = 0;// 替换内容时需进行修改
		m_w_content = Tools.getMidNum(Tools.getMaxWidth(m_menus), W_MIN_CONTENT, W_MAX_CONTENT) + 1;// 避免刚好相等的情况
		m_w_rect = m_w_content + (IW << 1);
		m_w = m_w_rect + (INITX_CONTENT << 1);
		m_strs = new String[menus.length][];
		/**
		 * 采用此种计算内容框高度的方式是基于以下两个原因 1：效率 2：在重新赋值的时候可以将原有的值带入目前的系统
		 */
		int sumH = 0;
		for (int i = 0; i < menus.length; i++) {
			if (menus[i] == null) {// 目前按空处理
				throw new IllegalArgumentException("未传入参数 index = " + i);
			}
			m_strs[i] = Tools.getStrArray(menus[i], AbstractGameScreen.s_font, m_w_content);
			sumH += m_strs[i].length * AbstractGameScreen.FONT_H + (IH << 1);
		}
		m_h_content = sumH;
		m_h = m_h_content + (INITY_CONTENT << 1);
		m_anchor = anchor;
		setCentre();
		
		registerTouch();
	}

	/* (non-Javadoc)
	 * @see com.handinfo.uimenu.UIAbstractWindow#registerTouch()
	 */
	protected void registerTouch() {
		clearAllRects();
		if (m_menus == null || m_menus.length == 0){
			return;
		}
		int len = m_menus.length;
		int[][]rect = new int[len+1][];
		int tempX = m_x + INITX_CONTENT;
		int tempY = m_y + INITY_CONTENT;
		int lineH = AbstractGameScreen.FONT_H + (IH<<1);
		for(int i=0;i<len+1;i++) {
			if (i == len) {
				rect[i] = new int[]{m_x, m_y, m_w, m_h, -1};
			} else {
				rect[i] = new int[]{tempX, tempY+i*lineH, m_w, lineH};
			}
		}
		initTouchRect(rect);
	}

	
	protected void onTouchRect(int[][] rect, int touchid) {
		if (touchid == m_strs.length) {
			GameTouchManager.keyPressedRSoft();
		} else {
			if (m_index_menu == touchid) {
				GameTouchManager.keyPressedFire();	
			} else {
				m_index_menu = touchid;
			}
		}
	}

	/**
	 * 获得当前索引
	 * @return
	 */
	public int getCurrentIndex()
	{
		return m_index_menu;
	}

	protected void keyLogic()
	{
		if (!m_isRuning) {
			return;
		}
		if (m_strs == null) {
			return;
		}
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_index_menu == 0) {
				m_index_menu = m_strs.length - 1;
			}
			else {
				m_index_menu--;
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_index_menu == m_strs.length - 1) {
				m_index_menu = 0;
			}
			else {
				m_index_menu++;
			}
		}
	}

	protected void paintContent(Graphics g)
	{
		if (m_strs == null) {
			return;
		}
		// UITools.drawWindowBackgroud(g, m_x, m_y, m_w-1, m_h-1);
		UITools.drawWindowBackgroud(g, m_x - 2, m_y - 2, m_w + 4, m_h + 4);
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		paintBox(g, m_x, m_y, m_w - 1, m_h - 1);
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		g.drawRect(m_x + 2, m_y + 2, m_w - 5, m_h - 5);
		//
		int tempX = m_x + INITX_CONTENT;
		int tempY = m_y + INITY_CONTENT;
		int contentX = tempX + IW;
		for (int i = 0, length = m_strs.length; i < length; i++) {
			if (m_strs[i] == null) {
				continue;
			}
			if (!m_isRuning) {
				if (i == m_index_menu) {
					UITools.paintYindaoDesxribe(g, tempX - 3, tempY - 3, m_w_rect + 6, m_strs[i].length
							* (AbstractGameScreen.FONT_H + (IH << 1) + 6), m_strs[i]);
				}
			}
			if (i == m_index_menu) {
				paintSelItem(g, tempX, tempY, m_w_rect, m_strs[i].length * AbstractGameScreen.FONT_H + (IH << 1));
				g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
			}
			else {
				g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
			}
			tempY += IH;// 相对框偏移三像素
			tempY = paintItem(g, m_strs[i], contentX, tempY);
			tempY += IH;
		}
	}

	private int paintItem(Graphics g, String[] strs, int x, int y)
	{
		int tempY = y;
		if (strs == null) {
			return tempY;
		}
		for (int j = 0; j < strs.length; j++) {
			if (strs[j] == null) {
				continue;
			}
			switch (m_anchor) {
				case ALIGN_LEFT:
					g.drawString(strs[j], x, tempY, Graphics.LEFT | Graphics.TOP);
					break;
				case ALIGN_CENTRE:
					g.drawString(strs[j], x +  (m_w_content >> 1), tempY, Graphics.HCENTER
							| Graphics.TOP);
					break;
				case ALIGN_RIGHT:
					g.drawString(strs[j], x + (m_w_content), tempY, Graphics.RIGHT | Graphics.TOP);
					break;
				default:
					throw new IllegalArgumentException("无此类型");
			}
			tempY += AbstractGameScreen.FONT_H;
		}
		return tempY;
	}

	private boolean m_isRuning = true;

	/**
	 * 2012-3-8 下午05:30:03 venizeng
	 */
	public void setStop()
	{
		m_isRuning = false;
	}

	/**
	 * 2012-3-8 下午05:30:21 venizeng
	 * @param eQUIP
	 */
	public void setStopMenu(Object item)
	{
		for (int i = 0; i < m_strs.length; i++) {
			if (m_strs[i] == item) {
				m_index_menu = i;
				return;
			}
		}
	}

	/**
	 * 2012-3-8 下午05:30:21 venizeng
	 * @param eQUIP
	 */
	public void setStopMenu(int index)
	{
		m_index_menu = index;
	}
}
